package com.googlecode.nacl_optimizer.elf.headers;

import com.googlecode.nacl_optimizer.elf.types.Elf32Word;


public enum SectionTypeField {
    SHT_NULL(new Elf32Word(0), "inactive"),
    SHT_PROGBITS(new Elf32Word(1), "program"),
    SHT_SYMTAB(new Elf32Word(2), "symbol table (static)"),
    SHT_STRTAB(new Elf32Word(3), "String Table"),
    SHT_RELA(new Elf32Word(4), "explicit relocation entries"),
    SHT_HASH(new Elf32Word(5), "symbol hash table"),
    SHT_DYNAMIC(new Elf32Word(6), "dynamic linking"),
    SHT_NOTE(new Elf32Word(7), "note section"),
    SHT_NOBITS(new Elf32Word(8), "NOBITS"),
    SHT_REL(new Elf32Word(9), "not explicit relocation entries"),
    SHT_SHLIB(new Elf32Word(10), "specified"),
    SHT_DYNSYM(new Elf32Word(11), "symbol table (dynamic)"),
    SHT_LOPROC(new Elf32Word(0x80000000), "lo processor-specific semantics"),
    SHT_HIPROC(new Elf32Word(0x7fffffff), "hi processor-specific semantics"),
    SHT_LOUSER(new Elf32Word(0x80000000), "lower bound for application programs"),
    SHT_HIUSER(new Elf32Word(0xffffffff), "upper bound for application programs"),
    PROC(new Elf32Word(0x7000000), "user"),
    USER(new Elf32Word(0x8000000), "proc");
    
    private String text;
    private int v;
    
    private SectionTypeField(Elf32Word v, String text) {
        this.v=v.getInteger();
        this.text=text;
    }

    public static SectionTypeField getType(Elf32Word word) {
        for(SectionTypeField type: SectionTypeField.values()) {
            if(word.getInteger().equals(type.v)) {
                return type;
            }
        }
        int v = word.getInteger();
        if( v > 0x7000000 && v < 0x7fffffff) {
            return PROC;
        }
        if( v > 0x8000000 && v < 0xffffffff) {
            return USER;
        }
        throw new IllegalArgumentException();
    }

    @Override
    public String toString() {
        return this.text;
    }
}
